﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using DbDoc.BusinessObjects;
using System.ComponentModel;

namespace DbDoc.PowerDoc.Model
{
    public class DbObjectModel : INotifyPropertyChanged
    {
        private string _Name;
        private ObservableCollection<DbObjectModel> _Children;
        private DbObjectModel _Parent;
        private DbObject _UnderlyingDbObject;
        
        public DbObject UnderlyingDbObject
        {
            get { return _UnderlyingDbObject; }
            set { _UnderlyingDbObject = value;
            OnPropertyChanged("UnderlyingDbObject");
            }
        }

        public Comment Comment
        {
            get
            {
                if (UnderlyingDbObject != null)
                    return UnderlyingDbObject.Comment;
                return null;
            }
            set
            {
                if (UnderlyingDbObject != null)
                    UnderlyingDbObject.Comment = value;
            }
        }

        public DbObjectModel Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public ObservableCollection<DbObjectModel> Children
        {
            get
            {
                if (_Children == null)
                    _Children = new ObservableCollection<DbObjectModel>();
                return _Children;
            }
            set
            {
                if (_Children == value) return;
                _Children = value;
            }
        }


        public DbObjectModel()
        {

        }

        public DbObjectModel(DbObject dbObject)
        {
            Name = dbObject.Name;
            UnderlyingDbObject = dbObject;
            if (UnderlyingDbObject.Children != null)
            {
                foreach (DbObject child in UnderlyingDbObject.Children)
                    Children.Add(new DbObjectModel(child));
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, args);
        }

        #endregion
    }
}
